#
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2023 Ricardo Pardini <ricardo@pardini.net>
# This file is a part of the Armbian Build Framework https://github.com/armbian/build/
#

# tl;dr: this function will return the version and download URL of a package.
# it is very naive, and will only look into a few repos across Debian/Ubuntu.
function apt_find_upstream_package_version_and_download_url() {
	declare sought_package_name="${1}"

	declare first_letter_of_sought_package_name="${sought_package_name:0:1}"
	declare mirror_with_slash="undetermined/"

	case "${DISTRIBUTION}" in
		Ubuntu)
			# Only LTS releases have an "-updates" repo that is worth looking into
			if [[ "${RELEASE}" == "focal" || "${RELEASE}" == "jammy" ]]; then # @TODO: release info information, is_ubuntu_release_lts() or similar
				package_download_release=${RELEASE}-updates
			else
				package_download_release=${RELEASE}
			fi
			mirror_with_slash="${UBUNTU_MIRROR}"
			;;

		Debian)
			package_download_release=${RELEASE}
			mirror_with_slash="${DEBIAN_MIRROR}"
			;;

		*)
			exit_with_error "Unknown distribution '${DISTRIBUTION}'"
			;;
	esac

	# if mirror_with_slash does not end with a slash, add it
	if [[ "${mirror_with_slash}" != */ ]]; then
		mirror_with_slash="${mirror_with_slash}/"
	fi

	declare base_down_url="http://${mirror_with_slash}pool/main/${first_letter_of_sought_package_name}/${sought_package_name}"
	# loong64 is using debian-ports repo, and download from pool-loong64
	[[ "${ARCH}" == "loong64" ]] && declare base_down_url="http://${mirror_with_slash}pool-loong64/main/${first_letter_of_sought_package_name}/${sought_package_name}"

	# get json with latest pacakge info generated by GHA
	case "${GITHUB_MIRROR}" in
		"ghproxy")
			package_info_download_url="https://${GHPROXY_ADDRESS}/https://raw.githubusercontent.com/armbian/armbian.github.io/refs/heads/data/data/${sought_package_name}.json"
			;;
		*)
			package_info_download_url="https://github.armbian.com/${sought_package_name}.json"
			;;
	esac
	package_info_download_url_file="$(mktemp)"
	curl --silent --show-error --max-time 10 $package_info_download_url -o $package_info_download_url_file
	found_package_filename=$(
		jq -r --arg release "${package_download_release}" --arg arch "${ARCH}" \
			'.[$release][$arch]' $package_info_download_url_file
	)

	if [[ "${found_package_filename}" == "${sought_package_name}_"* ]]; then
		display_alert "Found upstream base-files package filename" "${found_package_filename}" "info"
	else
		display_alert "Could not find package filename for '${sought_package_name}' in distro repo" "looking for ${sought_package_name}, found_package_filename is ${found_package_filename}" "warn"
		return 1
	fi

	found_package_down_url="${base_down_url}/${found_package_filename}"
	display_alert "Found package filename" "${found_package_filename} in url ${found_package_down_url}" "debug"

	# Now we have the package name, lets parse out the version.
	found_package_version="$(echo "${found_package_filename}" | grep -oP '(?<=_)[^_]+(?=_)')"
	display_alert "Found base-files upstream package version" "${found_package_version}" "info"

	# Sanity check...
	declare wanted_package_name="${sought_package_name}_${found_package_version}_${ARCH}.deb"
	if [[ "${found_package_filename}" != "${wanted_package_name}" ]]; then
		display_alert "Found package filename '${found_package_filename}' does not match wanted package name '${wanted_package_name}'" "looking for ${sought_package_name}" "warn"
		return 1
	fi

	# show found_package_down_url
	display_alert "Found package download url" "${found_package_down_url}" "debug"

	return 0
}
